package es.us.idinfor;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Calendar;
import java.util.Date;

public class Event implements Comparable<Event> {
	private static final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
	
	private String id;
	private long timeStamp;
	
	public Event(String name) {
		this(name, Calendar.getInstance().getTimeInMillis());
	}
	
	public Event(String name, String date) throws ParseException {
		this(name, sdf.parse(date).getTime());
	}
	
	public Event(String name, long miliseconds) {
		id = name;
		timeStamp = miliseconds;
	}
	
	public String getId() {
		return id;
	}
	
	public long getTimeStamp() {
		return timeStamp;
	}
	
	public int compareTo(Event o) {
		return (int) (getTimeStamp() - o.getTimeStamp());
	}
	
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		
		result = prime * result + ((getId() == null)? 0 : getId().hashCode());
		
		return result;
	}

	public boolean equals(Object obj) {
		if(this == obj) {
			return true;
		}
		
		if(obj == null) {
			return false;
		}
		
		if(getClass() != obj.getClass()) {
			return false;
		}
		
		Event other = (Event) obj;
		
		if(getId() == null) {
			if(other.getId() != null) {
				return false;
			}
		}
		else if(!getId().equals(other.getId())) {
			return false;
		}
		
		return true;
	}

	public String toString() {
		return "(" + getId() + " - " + sdf.format(new Date(getTimeStamp())) + ")";
	}
}
